package com.ztgf.commodity.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CategoryLabelRelationMapper;
import com.ztgf.commodity.models.dto.CategoryLabelRelationDTO;
import com.ztgf.commodity.models.service.ICategoryLabelRelationService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

@DubboService
public class CategoryLabelRelationServiceImpl extends ServiceImpl<CategoryLabelRelationMapper, CategoryLabelRelationDTO> implements ICategoryLabelRelationService {

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public void addRelationship(Long categoryId, String labelIds) {
        String[] ids = labelIds.split(",");
        for (String id : ids) {
            // 先查询是否存在
            Wrapper<CategoryLabelRelationDTO> wrapper = new EntityWrapper<>();
            wrapper.eq("category_id", categoryId).eq("label_id", Long.valueOf(id));
            CategoryLabelRelationDTO selectOne = this.selectOne(wrapper);
            if (null != selectOne) {
                continue;
            }
            CategoryLabelRelationDTO insertDto = new CategoryLabelRelationDTO();
            insertDto.setCategoryId(categoryId);
            insertDto.setLabelId(Long.valueOf(id));
            insertDto.setId(ymlConfig.getPkId());
            this.insert(insertDto);
        }
    }
}
